package com.jlastu.dlxy_androidapp.view.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.contract.LoginContract;
import com.jlastu.dlxy_androidapp.presenter.LoginPresenter;
import com.jlastu.dlxy_androidapp.utils.SpUtils;
import com.jlastu.dlxy_androidapp.view.MainActivity;

import java.util.Arrays;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, LoginContract.ILoginView {
    private LoginContract.ILoginPresenter mLoginPresenter;
    private EditText mUserNameEdit;
    private EditText mPassWordEdit;
    private SpUtils mSpUtils;
    private final String TAG = "LoginActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mLoginPresenter = new LoginPresenter(this, this);
        Button mLoginButton = findViewById(R.id.buttonLogin);
        mLoginButton.setOnClickListener(this);
        Button mRegisterButton = findViewById(R.id.buttonRegister);
        mRegisterButton.setOnClickListener(this);
        mUserNameEdit = findViewById(R.id.editTextUsername);
        mPassWordEdit = findViewById(R.id.editTextPassword);
        mSpUtils = new SpUtils(getApplicationContext());
        int user_id = mSpUtils.getInt("userid", 0);
        if (user_id != 0 ){
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            Log.d(TAG, "SavedUserID: " + user_id);
            finish();
        }
    }

    @Override
    public void showVerify(int verify, int id) {
        Log.d(TAG, "showVerify: " + verify);
        Log.d(TAG, "showUserId: " + id);

        if (verify == 1) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            mSpUtils.putInt("userid",id);
            Log.d(TAG, "setUserID: " + id);
            startActivity(intent);
            finish();

        } else if (verify == 2) {
            Toast.makeText(this, "用户名或密码错误，请检查后重试", Toast.LENGTH_SHORT).show();
        } else if (verify == 3) {
            Toast.makeText(this, "用户不存在，请前往注册", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.buttonLogin) {
            String username = mUserNameEdit.getText().toString();
            String password = mPassWordEdit.getText().toString();
            if (username.isEmpty()) {
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            } else if (password.isEmpty()) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();

            } else {
                mLoginPresenter.getUser(username, password);
            }
        } else if (id == R.id.buttonRegister) {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        }
    }

}
